package com.uum.oss.controller;

import cn.hutool.core.collection.CollUtil;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import com.uum.oss.pojo.entity.OssUpload;
import com.uum.oss.pojo.page.OssPage;
import com.uum.oss.pojo.vo.OssCatalogVO;
import com.uum.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Api(tags = "OSS图片接口")
@RestController
@RequestMapping("/api/v1/oss")
@RequiredArgsConstructor
public class OssController {

    private final OssService ossService;


    //上传文件到oss
    @Operation(description = "上传图片",businessType = BusinessType.IMPORT,isSaveRequestData = false)
    @ApiOperation("上传文件")
    @PostMapping("upload")
    public Result uploadOssFile(OssUpload upload){
        List<String> url = ossService.uploadFile(upload);
        return Result.success(url);
    }

    @ApiOperation("新建目录")
    @PostMapping("addOssCatalog/{catalogName}")
    public Result addOssCatalog(@PathVariable String catalogName){
        ossService.addOssCatalog(catalogName);
        return Result.success();
    }

    @ApiOperation("获取目录")
    @GetMapping("listOssCatalog")
    public Result listOssCatalog(){
        List<OssCatalogVO> list = ossService.listOssCatalog();
        return Result.success(list);
    }

    @Operation(description = "查询图片列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation("获取图片列表")
    @GetMapping("page")
    public Result getOssPicture(OssPage ossPage){
        List<String> result = ossService.page(ossPage);
        List<String> page = CollUtil.page(ossPage.getCurrentPage() - 1, ossPage.getPageSize(), result);
        ossPage.setPageList(page);
        ossPage.setTotal(result.size());
        return Result.success(ossPage);
    }

    @Operation(description = "删除图片",businessType = BusinessType.DELETE)
    @ApiOperation("批量删除图片")
    @DeleteMapping("delete")
    public Result deletePictureBatch(String ids){
        List<String> deleteList = ossService.deletePictureBatch(Arrays.asList(ids.split(",")));
        return Result.success(deleteList);
    }

}
